﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DBBase;
using System.Security;
using System.Security.Cryptography;


namespace Business
{
    public partial class Resource : Form
    {
        public Resource()
        {
            InitializeComponent();
            getjilu();
        }
        public void getjilu()
        {
            Query objquery = new Query();
            string ssqlex = "";
            ssqlex = string.Format(@"select * from ziyuankongzhi order by username ");
            DataTable dt = objquery.GetTable(ssqlex);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                com_mingchen.Items.Add(dt.Rows[i]["username"].ToString().Trim());
                if (i == 0)
                    com_mingchen.Text = dt.Rows[0]["username"].ToString().Trim();
            }
        }

        

       

        private void com_mingchen_SelectedIndexChanged(object sender, EventArgs e)
        {
            getneirong();
        }
        public void getneirong()
        {
            string sql = string.Format(@"Select * From ziyuankongzhi where username='{0}' ", com_mingchen.Text);

           Query mquery = new Query();
            DataTable dt = mquery.GetTable(sql);
            if (dt.Rows.Count > 0)
            {

                text_jianming.Text = dt.Rows[0]["keyname"].ToString().Trim();
                text_zhi.Text = dt.Rows[0]["keyzhi"].ToString().Trim();
                text_xsmc.Text = dt.Rows[0]["xsname"].ToString().Trim();
                com_zhuangtai.Text = dt.Rows[0]["zhuangtai"].ToString().Trim();
                text_beizhu.Text = dt.Rows[0]["beizhu"].ToString().Trim();
                

            }
        }
        /// <summary>
        /// 是否为日期型字符串
        /// </summary>
        /// <param name="StrSource">日期字符串(2008-05-08)</param>
        /// <returns></returns>
        public static bool IsDate(string StrSource)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(StrSource, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-9]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
        }
        /// <summary>
        /// 是否为时间型字符串
        /// </summary>
        /// <param name="source">时间字符串(15:00:00)</param>
        /// <returns></returns>
        public static bool IsTime(string StrSource)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(StrSource, @"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
        }

        private void but_save_Click(object sender, EventArgs e)
        {
            if (text_beizhu.Text.Contains("正整数"))
            {

                bool isNumber = System.Text.RegularExpressions.Regex.IsMatch(text_zhi.Text, @"^[1-9]\d*$"); 
                if (isNumber)
                { // 是正整数 
                    
                } 
                else 
                { // 不是 
                    MessageBox.Show("输入值不为正整数");
                    return;
                }

               
            }
            if (text_beizhu.Text.Contains("yyyy-mm-dd"))
            {
                if (!IsDate(text_zhi.Text))
                {
                     MessageBox.Show("输入值不为日期");
                     return;
                }
            }

            if (text_beizhu.Text.Contains("hh:mm:ss"))
            {
                try
                {
                    if (!IsTime(text_zhi.Text.Split(new char[] { '~' })[0]))
                    {
                        MessageBox.Show("输入时间段不正确");
                        return;
                    }
                    if (!IsTime(text_zhi.Text.Split(new char[] { '~' })[1]))
                    {
                        MessageBox.Show("输入时间段不正确");
                        return;
                    }
                }
                catch { }
               
            }

            Query objquery = new Query();
            string ssql = "";
            ssql = string.Format(@"Select count(*) From ziyuankongzhi where username='{0}'", com_mingchen.Text);
            if (objquery.ExecuteS(ssql))
            {
                ssql = string.Format(@"update  ziyuankongzhi 
                                       set keyname='{0}' ,
                                        keyzhi='{1}' ,
                                        xsname='{2}' ,
                                        zhuangtai='{3}' ,
                                        beizhu='{4}' where username='{5}'",
                                                text_jianming.Text,
                                                text_zhi.Text,
                                                text_xsmc.Text,
                                                com_zhuangtai.Text,
                                                text_beizhu.Text,
                                                com_mingchen.Text);
            }
            else
            {
                ssql = string.Format(@"insert into ziyuankongzhi (
                                        username,
                                        keyname,
                                        keyzhi,
                                        xsname,
                                        zhuangtai,
                                        beizhu) values 
                                        ('{0}',
                                        '{1}',
                                        '{2}',
                                        '{3}',
                                        '{4}',
                                        '{5}')", com_mingchen.Text,
                                                text_jianming.Text,
                                                text_zhi.Text,
                                                text_xsmc.Text,
                                                com_zhuangtai.Text,
                                                text_beizhu.Text);
            }
            if (!objquery.UpdateTable(ssql))
            {
                MessageBox.Show("添加失败");
            }
            else
            {
                MessageBox.Show("添加成功");
            }
        }

        private void but_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
